#pragma once
#include <lvgl.h>
#include <gui.hpp>

/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/

// ---------------- 资源声明 ----------------
extern "C" {
    // 字体声明
    LV_FONT_DECLARE(lv_customer_font_SourceHanSerifSC_Regular_13)
    /*!USER_DECLARE_BEGIN!*/
    /*!USER_DECLARE_END!*/
    // 图片声明
    LV_IMG_DECLARE(_dianzisheji_alpha_76x50)
    /*!USER_DECLARE_BEGIN!*/
    /*!USER_DECLARE_END!*/
    // 其他
    /*!USER_DECLARE_BEGIN!*/
    /*!USER_DECLARE_END!*/
}

// ---------------- 组件定义 ----------------
namespace gui::widgets::main {
    using namespace gui::compose;
    inline Component screen;
	inline Label screen_label_D;
	inline Label screen_label_D_data;
	inline Button screen_btn_1;
	inline Label screen_btn_1_label;
	inline Image screen_img_1;
	inline Label screen_label_Vm;
	inline Label screen_label_tr;
	inline Label screen_label_fo;
	inline Label screen_label_fo_data;
	inline Label screen_label_Vm_data;
	inline Label screen_label_tr_data;
    /*!USER_DECLARE_BEGIN!*/
    /*!USER_DECLARE_END!*/
}

// ================== 用户空间 ==================
/*!USER_DECLARE_BEGIN!*/

/*!USER_DECLARE_END!*/

// ---------------- UI接口 ----------------
namespace gui::interface {
    using namespace gui::widgets::main;
    /*!USER_DECLARE_BEGIN!*/
class PPM
{
public:
    static  auto generate_text(const float fo, const float D, const float Vm, const float tr)
    {
        auto set_label = [](auto& label, const char* fmt, auto value) {
            char buf[24]; // 足够容纳任何浮点数
            snprintf(buf, sizeof(buf), fmt, value);
            label.text(buf);
        };

        // char buf[24]; // 足够容纳任何浮点数
        // snprintf(buf, sizeof(buf), "%.3f Hz", fo);
        // screen_label_fo_data.text(buf);

        set_label(screen_label_fo_data, "%.3fHz", fo);
        set_label(screen_label_D_data, "%.3f%%", D);
        set_label(screen_label_Vm_data, "%.3fmV", Vm);
        set_label(screen_label_tr_data, "%.3fus", tr);

    }



};

    /*!USER_DECLARE_END!*/
}

// ---------------- 初始化逻辑 ----------------
namespace gui
{
    class ui
    {
    public: 
        static void screenInit() {
            using namespace gui::widgets::main;
            
            screen.init(nullptr);
			screen.size(480,320);
			screen.bg_opa(255);
			

			screen_label_D.init(screen);
			screen_label_D.pos(26,47);
			screen_label_D.size(48,18);
			screen_label_D.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_D.bg_opa(0);
			lv_label_set_text(screen_label_D,"占空比:");
			lv_obj_set_style_text_font(screen_label_D,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_label_D_data.init(screen);
			screen_label_D_data.pos(143,47);
			screen_label_D_data.size(97,19);
			screen_label_D_data.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_D_data.bg_opa(0);
			lv_label_set_text(screen_label_D_data,"0");
			lv_obj_set_style_text_font(screen_label_D_data,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_btn_1.init(screen);
			screen_btn_1.pos(26,187);
			screen_btn_1.size(51,32);
			screen_btn_1.bg_opa(255);
			screen_btn_1.bg_color(lv_color_hex(0x2195f6));
			screen_btn_1.radius(5);
			screen_btn_1.text_color(lv_color_hex(0xffffff));
			screen_btn_1.text_align(LV_TEXT_ALIGN_CENTER);
			lv_obj_set_style_pad_all(screen_btn_1,0,LV_STATE_DEFAULT);
			lv_obj_set_style_text_font(screen_btn_1,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_btn_1_label.init(screen_btn_1);
			screen_btn_1_label.center(0,0);
			lv_label_set_text(screen_btn_1_label,"测量");
			lv_obj_set_width(screen_btn_1_label,LV_PCT(100));

			screen_img_1.init(screen);
			screen_img_1.pos(402,3);
			screen_img_1.size(76,50);
			lv_obj_add_flag(screen_img_1,LV_OBJ_FLAG_CLICKABLE);
			lv_img_set_src(screen_img_1,&_dianzisheji_alpha_76x50);
			lv_img_set_pivot(screen_img_1,50,50);
			lv_img_set_angle(screen_img_1,0);
			lv_obj_set_style_img_recolor_opa(screen_img_1,0,selector_default);
			lv_obj_set_style_img_opa(screen_img_1,255,selector_default);
			lv_obj_set_style_clip_corner(screen_img_1,true,selector_default);

			screen_label_Vm.init(screen);
			screen_label_Vm.pos(26,115);
			screen_label_Vm.size(92,19);
			screen_label_Vm.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_Vm.bg_opa(0);
			lv_label_set_text(screen_label_Vm,"脉冲信号幅度：");
			lv_obj_set_style_text_font(screen_label_Vm,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_label_tr.init(screen);
			screen_label_tr.pos(26,147);
			screen_label_tr.size(117,19);
			screen_label_tr.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_tr.bg_opa(0);
			lv_label_set_text(screen_label_tr,"脉冲信号上升时间：");
			lv_obj_set_style_text_font(screen_label_tr,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_label_fo.init(screen);
			screen_label_fo.pos(26,84);
			screen_label_fo.size(67,18);
			screen_label_fo.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_fo.bg_opa(0);
			lv_label_set_text(screen_label_fo,"信号频率:");
			lv_obj_set_style_text_font(screen_label_fo,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_label_fo_data.init(screen);
			screen_label_fo_data.pos(143,84);
			screen_label_fo_data.size(99,19);
			screen_label_fo_data.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_fo_data.bg_opa(0);
			lv_label_set_text(screen_label_fo_data,"0");
			lv_obj_set_style_text_font(screen_label_fo_data,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_label_Vm_data.init(screen);
			screen_label_Vm_data.pos(143,115);
			screen_label_Vm_data.size(95,19);
			screen_label_Vm_data.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_Vm_data.bg_opa(0);
			lv_label_set_text(screen_label_Vm_data,"0");
			lv_obj_set_style_text_font(screen_label_Vm_data,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

			screen_label_tr_data.init(screen);
			screen_label_tr_data.pos(143,147);
			screen_label_tr_data.size(100,19);
			screen_label_tr_data.text_align(LV_TEXT_ALIGN_LEFT);
			screen_label_tr_data.bg_opa(0);
			lv_label_set_text(screen_label_tr_data,"0");
			lv_obj_set_style_text_font(screen_label_tr_data,&lv_customer_font_SourceHanSerifSC_Regular_13,selector_default);

            /*!USER_DECLARE_BEGIN!*/

/*!USER_DECLARE_END!*/
            lv_obj_update_layout(screen);// 更新布局
            lv_scr_load(screen);// 加载主屏幕
        }
        
        static void eventInit() {
            using namespace gui::widgets::main;
            /*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/
        }
    };
}

/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/
